<template>
  <div class="app-container">
    <h3 class="student-manage-title">学生管理</h3>
    <div class="mainbox">
      <div class="table-container table-fullscreen">
        <div class="table-opt-container student-container" style="flex-direction: row">
          <el-button type="primary" icon="el-icon-plus"  @click="handleAdd">添加学生</el-button>
        </div>
        <el-row class="studentRow">
          <el-col :span="8" v-for="(info, index) in infoList" :key="info.id"
                  :offset="index % 2 > 0 ? 2 : 0">
            <el-card :body-style="{ padding: '0px' }">
              <div class="studentImg">
                <el-image>
                  <div slot="error" class="">
                    <img src="@/framework/assets/user_f.jpeg" alt="" class="user-avatar">
                  </div>
                </el-image>
                <span id="studentName" class="studentName">{{ info.studentName }}</span>
              </div>
              <div class="student-button">
                <div style="font-size: 13px;">累计视频学习次数</div>
                <div class="studyNum">{{formatNumber(info.studyNum)}}</div>
                <div style="padding: 15px;">
                  <el-button type="text" @click="startLearning(info)" class="button">开始学习</el-button>
                  <el-divider direction="vertical"></el-divider>
                  <el-button type="text" @click="historyInfo(info)" class="button">学习记录</el-button>
                </div>
                <el-divider class="studentDivider"></el-divider>
                <div style="padding: 5px;">
                  <el-button type="text" icon="el-icon-edit" @click="handleUpdate(info)"
                             style="margin-right: 16%;">编辑</el-button>
                  <el-divider direction="vertical"></el-divider>
                  <el-button type="text" icon="el-icon-delete" @click="handleDelete(info)"
                             style="margin-left: 16%;">删除</el-button>
                </div>
              </div>
            </el-card>
          </el-col>
        </el-row>
      </div>
    </div>
    <!-- 添加或修改对话框 -->
    <dt-dialog :title="title" :visible.sync="open" :is-loading="loading"
               @comfirmBtn="submitForm" @closeDialog="cancel">
      <div slot="content">
        <el-form ref="form" :model="form" :rules="rules"
                 label-width="100px">
          <el-form-item label="学生姓名" prop="studentName">
            <el-input v-model.trim="form.studentName" placeholder="请填写学生姓名" maxlength="30"
                      show-word-limit class="limit" />
          </el-form-item>
        </el-form>
      </div>
    </dt-dialog>
  </div>
</template>

<script>
// eslint-disable-next-line import/no-cycle
import { listInfo, delInfo, addInfo, updateInfo } from '@/api/onlineClass/studentInfo'
import { mapGetters } from 'vuex'

export default {
  name: 'studentInfo',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      infoList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10
      },
      // 表单参数
      form: {
        id: null,
        studentName: null
      },
      // 表单校验
      rules: {
        studentName: [{ required: true, message: '请填写学生姓名', trigger: 'blur' }]
      }
    }
  },
  computed: {
    ...mapGetters(['onlineStudy', 'studentInfo'])
  },
  mounted() {
    this.getList()
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      listInfo(this.queryParams).then(({ data: response }) => {
        this.infoList = response
        // this.total = response.totalRows
        this.loading = false
      })
    },
    // 格式化数字，用千分位分割
    formatNumber(num) {
      let str = num.toString()
      let result = ''
      while (str.length > 3) {
        result = `,${str.slice(-3)}${result}`
        str = str.slice(0, str.length - 3)
      }
      if (str) {
        result = str + result
      }
      return result
    },
    // 跳转开始学习
    startLearning(row) {
      this.$store.commit('classroom/CHANGE_STUDENT_INFO', row)
      this.$router.push({
        path: '/onlineClassroom/course',
        query: {
          studentId: row.id,
          studentName: row.studentName,
          menuPath: this.onlineStudy[0].path
        }
      })
    },
    // 跳转学习记录
    historyInfo(row) {
      this.$store.commit('classroom/CHANGE_STUDENT_INFO', row)
      if (row.studyNum > 0) {
        this.$router.push({ path: '/onlineClassroom/browsingHistory', query: { studentId: row.id, studentName: row.studentName } })
      } else {
        this.$message.warning('当前还没有学习记录，请开始学习吧！')
      }
    },
    // 取消按钮
    cancel() {
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        studentName: null
      }
      this.resetForm('form')
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加学生'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      this.form.id = row.id
      this.form.studentName = row.studentName
      this.open = true
      this.title = '修改学生'
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateInfo(this.form).then((response) => {
              this.$dtModal.msgSuccess('修改成功')
              this.open = false
              this.getList()
              this.$store.dispatch('classroom/changeStudentList')
            })
          } else {
            addInfo(this.form).then((response) => {
              this.$dtModal.msgSuccess('新增成功')
              this.open = false
              this.getList()
              this.$store.dispatch('classroom/changeStudentList')
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const { id } = row
      this.$dtModal.confirm(`是否确认删除学生【"${row.studentName}"】的数据项？`).then(() => {
        return delInfo(id)
      }).then(() => {
        this.getList()
        this.$store.dispatch('classroom/changeStudentList')
        this.$dtModal.msgSuccess('删除成功')
      }).catch(() => {})
    }
  }
}
</script>

<style scoped>
.studentImg {
  height: 50px;
}
.studentImg .el-image {
  vertical-align: middle;
  margin-top: 3px;
  margin-left: 20px;
}
.student-manage-title {
    font-family: "Source Han Sans CN Medium";
    font-weight: 600;
    font-size: 24px;
    text-align: left;
    color: #00080f;
    margin-block-start: 0;
    margin-block-end: 0;
    margin-left: 20px;
    margin-top: 20px;
}
::v-deep .studentImg .user-avatar {
  cursor: pointer;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  margin: 10px;
}
::v-deep .studentImg .studentName {
  margin: 0 8px;
  font-size: 15px;
  font-weight: bold;
  vertical-align: middle;
}
::v-deep .student-container {
  flex-direction: row-reverse;
}
.student-button {
  margin: auto;
  text-align: center;
  color: #989898;
}
.student-button .el-button {
  color: #989898;
}
.student-button .studyNum {
  font-size: 33px;
  /* font-weight: bold; */
  margin-top: 10px;
  color: #666666;
}
.studentRow .el-col {
  margin-left: 30px;
  margin-top: 15px;
}
.studentRow .el-col-offset-2 {
  margin-left: 50px;
}
.studentDivider {
  margin-top: 0;
  margin-bottom: 0;
}
</style>
